/**
 * 
 */
package tests.board;

import java.util.ArrayList;

import junit.framework.TestCase;
import root.JChess;
import root.board.EmptyPlaceException;
import root.board.Piece;
import root.board.moviment.Moviment;
import root.board.moviment.Position;
import root.board.pieces.King;

/**
 * @author Calebe
 * 
 */
public class GetEnabledMovimentsKing extends TestCase {
	private JChess jchess;

	public void testEatPiece() {
		jchess = new JChess();
		jchess.newGame();
		ArrayList<Moviment> moviments = new ArrayList<Moviment>();
		try {
			if (jchess.isPiecePresent(new Position(2, 1)))
				jchess.move(new Position(2, 1), new Position(3, 0));
			if (jchess.isPiecePresent(new Position(6, 1)))
				jchess.move(new Position(6, 1), new Position(5, 0));
			if (jchess.isPiecePresent(new Position(4, 1)))
				jchess.getPieceOn(new Position(4, 1)).setTeam(Piece.SOUTH);

			moviments = jchess.getEnabledMoviments(new Position(4, 0));

		} catch (EmptyPlaceException e) {
			e.printStackTrace();
		}
		assertEquals(1, moviments.size());
	}

	public void testFreeKing() {
		jchess = new JChess();
		jchess.newGame();

		Position kingPosition = new Position(4, 0);
		ArrayList<Moviment> moviments = new ArrayList<Moviment>();
		if (jchess.isPiecePresent(kingPosition)) {
			try {
				((King) (jchess.getPieceOn(kingPosition))).setMoved(true);
				Position destinyPosition = new Position(4, 4);
				Moviment movivent = new Moviment(kingPosition, destinyPosition);
				jchess.move(movivent);
				moviments = jchess.getEnabledMoviments(destinyPosition);
			} catch (EmptyPlaceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		assertEquals(8, moviments.size());
	}

	public void testFullBlockedKing() {
		jchess = new JChess();
		jchess.newGame();
		ArrayList<Moviment> moviments = new ArrayList<Moviment>();
		try {
			jchess.move(new Position(2, 1), new Position(3, 0));
			jchess.move(new Position(6, 1), new Position(5, 0));
			moviments = jchess.getEnabledMoviments(new Position(4, 0));
		} catch (EmptyPlaceException e) {
			e.printStackTrace();
		}
		assertEquals(0, moviments.size());
	}

	public void testLeftCastling() {
		jchess = new JChess();
		jchess.newGame();
		ArrayList<Moviment> moviments = new ArrayList<Moviment>();
		try {
			if (jchess.isPiecePresent(new Position(1, 0)))
				jchess.move(new Position(1, 0), new Position(4, 4));

			if (jchess.isPiecePresent(new Position(2, 0)))
				jchess.move(new Position(2, 0), new Position(4, 4));

			if (jchess.isPiecePresent(new Position(3, 0)))
				jchess.move(new Position(3, 0), new Position(4, 4));

			if (jchess.isPiecePresent(new Position(6, 1)))
				jchess.move(new Position(6, 1), new Position(5, 0));

			moviments = jchess.getEnabledMoviments(new Position(4, 0));

		} catch (EmptyPlaceException e) {
			e.printStackTrace();
		}
		assertEquals(1, moviments.size());
	}

	public void testRigthCastling() {
		jchess = new JChess();
		jchess.newGame();
		ArrayList<Moviment> moviments = new ArrayList<Moviment>();
		try {
			if (jchess.isPiecePresent(new Position(6, 0)))
				jchess.move(new Position(6, 0), new Position(4, 4));

			if (jchess.isPiecePresent(new Position(5, 0)))
				jchess.move(new Position(5, 0), new Position(4, 4));

			if (jchess.isPiecePresent(new Position(2, 1)))
				jchess.move(new Position(2, 1), new Position(3, 0));

			moviments = jchess.getEnabledMoviments(new Position(4, 0));

		} catch (EmptyPlaceException e) {
			e.printStackTrace();
		}
		assertEquals(1, moviments.size());
	}
}
